package org.balabala.gateway.redis;

import org.balabala.gateway.entity.User;
import org.balabala.gateway.entity.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class UserManager {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private Jackson2JsonRedisSerializer<User> userJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(User.class);
    private Jackson2JsonRedisSerializer<Video> videoJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Video.class);


    private static final String USER_KEY = "users";
    private static final String VIDEO_KEY = "user_videos";
    private static final String FAVOURITE_KEY = "user_favourite";
    private static final String HISTORY_KEY = "user_history";

    private static final long TWO_HOUR = 2*60*60;

    /**
     * 根据uid获取用户信息
     * @param key
     * @return
     */
    public User getUser(Long key){
        redisTemplate.setHashValueSerializer(userJackson2JsonRedisSerializer);
        System.out.println(redisTemplate.opsForHash().get(USER_KEY, key));
        return (User) redisTemplate.opsForHash().get(USER_KEY,key);
    }

    /**
     * 添加用户信息
     * @param user
     */
    public void setUser(User user){
        if (user == null){
            return;
        }
        redisTemplate.setHashValueSerializer(userJackson2JsonRedisSerializer);
        redisTemplate.opsForHash().put(USER_KEY,user.getUid(),user);
    }

    /**
     * 删除用户信息
     * @param uid
     * @return
     */
    public Long deleteUser(Long uid){
        if (uid == null){
            return null;
        }
        return redisTemplate.opsForHash().delete(USER_KEY,uid);
    }


    /**
     * 设置用户发布的视频
     * @param uid
     * @param video
     */
    public void setUserVideos(Integer uid, Video video){
        redisTemplate.setValueSerializer(videoJackson2JsonRedisSerializer);
        String key = VIDEO_KEY+uid;
        redisTemplate.opsForList().leftPush(key,video);
    }


    /**
     * 獲取用戶發布的視頻
     * @param uid
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Object> getUserVideos(Integer uid){
        if (uid == null || !redisTemplate.hasKey(String.valueOf(uid))){
            return null;
        }
        redisTemplate.setValueSerializer(videoJackson2JsonRedisSerializer);
        String key = VIDEO_KEY+uid;
        return redisTemplate.opsForList().range(key,0,-1);
    }

    /**
     * 添加用戶喜歡的視頻
     * @param uid
     * @param video
     */
    public void setUserFavourite(Integer uid, Video video){
        if (uid == null){
            return;
        }
        String key = FAVOURITE_KEY+uid;
        redisTemplate.opsForList().leftPush(key,video);
    }

    /**
     * 獲取用戶喜歡的視頻
     */
    @SuppressWarnings("unchecked")
    public List<Object> getUserFavourites(Integer uid){
        if (uid == null || !redisTemplate.hasKey(String.valueOf(uid))){
            return null;
        }
        redisTemplate.setValueSerializer(videoJackson2JsonRedisSerializer);
        String key = FAVOURITE_KEY+uid;
        return redisTemplate.opsForList().range(key,0,-1);
    }

    public List<Object> getUserHistory(Long uid){
        redisTemplate.setValueSerializer(videoJackson2JsonRedisSerializer);
        String key = HISTORY_KEY+uid;
        return redisTemplate.opsForList().range(key,0,10);
    }

    /**
     * 添加前十條數據到redis
     * @param uid
     * @param videos
     */
    public void addUserHistory(Long uid, Object[] videos){
        redisTemplate.setValueSerializer(videoJackson2JsonRedisSerializer);
        String key = HISTORY_KEY+uid;
        if (videos.length > 10){
            Video[] videos1 = new Video[10];
            System.arraycopy(videos,0,videos1,0,10);
        }
        redisTemplate.opsForList().leftPushAll(key,videos);
        redisTemplate.expire(key,TWO_HOUR, TimeUnit.SECONDS);
    }


    public Boolean containsKey(String key){
        return redisTemplate.hasKey(key);
    }

}
